package site.marqstree.latte.ec.sign;

import android.app.Activity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.textfield.TextInputEditText;

import butterknife.BindView;
import butterknife.OnClick;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.R2;
import site.marqstree.latte.ec.profile.AppProfile;
import site.marqstree.latte.core.delegates.LatteDelegate;
import site.marqstree.latte.core.net.RestClient;
import site.marqstree.latte.core.net.callback.ISuccess;
import site.marqstree.latte.core.util.log.LatteLogger;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.sign
 * 文件名: SignUpDelegate
 * 创建者: marqstree
 * 创建时间: 2020/1/19 8:25
 * 描述: 注册页面
 */
public class SignUpDelegate extends LatteDelegate {

    @BindView(R2.id.edit_sign_up_name)
    TextInputEditText mName;
    @BindView(R2.id.edit_sign_up_email)
    TextInputEditText mEmail;
    @BindView(R2.id.edit_sign_up_phone)
    TextInputEditText mPhone;
    @BindView(R2.id.edit_sign_up_password)
    TextInputEditText mPassword;
    @BindView(R2.id.edit_sign_up_re_password)
    TextInputEditText mRePassword;

    //注册成功回调接口，在主Activity中实现
    private ISignListener mISignListener = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_up;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

    }

    //获取Activity的回调接口
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ISignListener) {
            mISignListener = (ISignListener) activity;
        }
    }

    //验证输入正确性
    private boolean checkForm() {
        //获取输入
        final String name = mName.getText().toString();
        final String email = mEmail.getText().toString();
        final String phone = mPhone.getText().toString();
        final String password = mPassword.getText().toString();
        final String rePassword = mRePassword.getText().toString();

        //验证通过?
        boolean isPass = true;

        if (name.isEmpty()) {
            //设置错误提示
            mName.setError("请输入姓名");
            isPass = false;
        } else {
            mName.setError(null);
        }

        if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            //设置错误提示
            mEmail.setError("错误的邮箱格式");
            isPass = false;
        } else {
            mEmail.setError(null);
        }

        if (phone.isEmpty() || phone.length() != 11) {
            //设置错误提示
            mPhone.setError("手机号码错误");
            isPass = false;
        } else {
            mPhone.setError(null);
        }

        if (password.isEmpty() || password.length() < 6) {
            //设置错误提示
            mPassword.setError("请填写至少6位数密码");
            isPass = false;
        } else {
            mPassword.setError(null);
        }

        if (rePassword.isEmpty() || rePassword.length() < 6 || !(rePassword.equals(password))) {
            //设置错误提示
            mRePassword.setError("密码验证错误");
            isPass = false;
        } else {
            mRePassword.setError(null);
        }

        return isPass;
    }

    @OnClick(R2.id.btn_sign_up)
    public void onViewClicked() {

        if (!checkForm())
            return;

        RestClient.builder()
                .url(AppProfile.SIGN_UP_URL)
                .params("name", mName.getText().toString())
                .params("email", mEmail.getText().toString())
                .params("phone", mPhone.getText().toString())
                .params("password", mPassword.getText().toString())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        LatteLogger.json("USER_PROFILE", response);
                        SignHandler.onSignUp(response,mISignListener);
                    }
                })
                .build()
                .post();

    }
}
